package dao;
import java.util.List;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import util.HibernateUtil;
import entity.Medico;

public class MedicoDaoImplementation implements MedicoDao {

	public void save(Medico medico) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.save(medico);
		transaction.commit();
		session.close();
	}

	public void update(Medico medico) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.update(medico);
		transaction.commit();
		session.close();
	}

	public void delete(Medico medico) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.delete(medico);
		transaction.commit();
		session.close();
	}

	public void consult(Medico medico) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		medico = (Medico) session.load(Medico.class, new String("Medico"));
	}

	public Medico getMedico(long id) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		return (Medico) session.load(Medico.class, id);
	}
	
	public List<Medico> list() 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        String sql = "SELECT new Medico( m.idMedico, m.nome, m.apelido, m.crm, m.cidade) FROM Medico m";
        List<Medico> lista = session.createQuery(sql).list();
        transaction.commit();
        session.close();
        return lista;
	}
	
	

}
